<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
        ref="queryFormRef"
        :inline="true"
        :model="queryParams"
        class="-mb-15px"
        label-width="85px"
    >
          <el-form-item label="项目编号" prop="projectNumber">
            <el-input
                v-model="queryParams.projectNumber"
                placeholder="请输入项目编号"
                clearable
                @keyup.enter="handleQuery"
                class="!w-140px"
            />
          </el-form-item>
          <el-form-item label="纳税人属性" prop="taxpayerAttribute" >
            <el-select
                v-model="queryParams.taxpayerAttribute"
                placeholder="请选择纳税人属性"
                clearable
                class="!w-140px"
            >
              <el-option
                  v-for="dict in getIntDictOptions(DICT_TYPE.OA_TAXPAYER_ATTRIBUTE)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="重点企业" prop="isKeyEnterprise">
            <el-select
                v-model="queryParams.isKeyEnterprise"
                placeholder="请选择是否为重点企业"
                clearable
                class="!w-140px"
            >
              <el-option
                  v-for="dict in getIntDictOptions(DICT_TYPE.OA_IS_OR_NO)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="公司名称" prop="companyName">
            <el-input
                v-model="queryParams.companyName"
                placeholder="请输入公司名称"
                clearable
                @keyup.enter="handleQuery"
                class="!w-140px"
            />
          </el-form-item>
          <el-form-item label="审批结果" prop="status">
            <el-select
                v-model="queryParams.status"
                placeholder="请选择审批结果"
                clearable
                class="!w-140px"
            >
              <el-option
                  v-for="dict in getIntDictOptions(DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="项目状态" prop="projectStatus">
            <el-select
                v-model="queryParams.projectStatus"
                placeholder="请选择项目状态"
                clearable
                class="!w-140px"
            >
              <el-option
                  v-for="dict in getIntDictOptions(DICT_TYPE.OA_PROJECT_STATUS)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
        <el-button plain type="primary" @click="handleCreate()">
          <Icon class="mr-5px" icon="ep:plus" />
          新增
        </el-button>
        <!-- <el-upload
          :show-file-list="false"
          :before-upload="beforeImport"
          accept=".xlsx,.xls"
          v-hasPermi="['oa:project-management:import']"
        >
          <el-button plain type="success">
            <Icon class="mr-5px" icon="ep:upload" />
            导入
          </el-button>
        </el-upload> -->
            </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="项目编号" align="center" prop="projectNumber" min-width="220" />
      <el-table-column label="公司名称" align="center" prop="companyName" min-width="250" />
      <el-table-column label="法人姓名" align="center" prop="legalPerson" width="100" />
      <el-table-column label="法人电话" align="center" prop="legalPersonPhone" width="120" />
      <el-table-column label="招商负责人" align="center" prop="investmentPromotionOfficer" width="100" />
      <el-table-column label="注册园区及平台" align="center" prop="registeredParkPlatform" min-width="150" />
      
      <el-table-column label="项目类型" align="center" prop="projectType" width="140">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OA_PROJECT_TYPE" :value="scope.row.projectType" />
        </template>
      </el-table-column>
      <el-table-column label="企业联系人" align="center" prop="enterpriseContact" width="100" />
      <el-table-column label="企业电话" align="center" prop="enterpriseContactPhone" width="120" />
      <el-table-column label="介绍人或下级平台" align="center" prop="introducerOrSubPlatform" min-width="150" />
      <el-table-column label="申请人" align="center" prop="userName" width="100" />
      <el-table-column label="项目状态" align="center" prop="projectStatus" width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OA_PROJECT_STATUS" :value="scope.row.projectStatus" />
        </template>
      </el-table-column>
      <el-table-column label="纳税人属性" align="center" prop="taxpayerAttribute" width="110">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OA_TAXPAYER_ATTRIBUTE" :value="scope.row.taxpayerAttribute" />
        </template>
      </el-table-column>
      <el-table-column label="收费金额" align="center" prop="serviceFee" width="100" />
      
      <el-table-column label="税收情况" align="center" prop="taxSituation" min-width="150" />
      <el-table-column label="返税比例" align="center" prop="taxRefundRatio" min-width="150" />
      <el-table-column label="项目确认人" align="center" prop="registrantName" width="100" />
      <!-- <el-table-column label="是否为简化企业" align="center" prop="isSimplifiedEnterprise" width="120">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OA_IS_OR_NO" :value="scope.row.isSimplifiedEnterprise" />
        </template>
      </el-table-column>
      <el-table-column label="是否为重点企业" align="center" prop="isKeyEnterprise" width="120">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OA_IS_OR_NO" :value="scope.row.isKeyEnterprise" />
        </template>
      </el-table-column>
      
      
      
      <el-table-column label="是否计算项目数" align="center" prop="isCountedProject" width="120">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OA_IS_OR_NO" :value="scope.row.isCountedProject" />
        </template>
      </el-table-column> -->
      
      
      
      
      <el-table-column label="审批结果" align="center" prop="status" width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      
      <!-- <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" /> -->
    <el-table-column label="操作" align="center" width="180" fixed="right">
      <template #default="scope">
        <el-button
            v-hasPermi="['oa:project-management:query']"
            link
            type="primary"
            @click="handleDetail(scope.row)"
        >
          详情
        </el-button>
        <el-button
            v-hasPermi="['oa:project-management:query']"
            link
            type="primary"
            @click="handleProcessDetail(scope.row)"
        >
          进度
        </el-button>
        
      </template>
    </el-table-column>
  </el-table>
    <!-- 分页 -->
    <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </ContentWrap>

  <!-- 完成项目弹窗 -->
  <CompleteProjectDialog ref="completeDialogRef" @success="handleCompleteSuccess" />
</template>
<script lang="ts" setup>
  import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  import { dateFormatter } from '@/utils/formatTime'
    import download from '@/utils/download'
  import { ProjectManagementApi, ProjectManagementVO } from '@/api/oa/projectmanagement'
  import * as ProcessInstanceApi from '@/api/bpm/processInstance'
  import { useUserStore } from '@/store/modules/user'
  import CompleteProjectDialog from './CompleteProjectDialog.vue'
  
  defineOptions({ name: 'ProjectManagementTodo' })

  const message = useMessage() // 消息弹窗
  const router = useRouter() // 路由
  const { t } = useI18n() // 国际化
  const userStore = useUserStore() // 用户store

  const loading = ref(true) // 列表的加载中
  const total = ref(0) // 列表的总页数
  const list = ref([]) // 列表的数据
  const completeDialogRef = ref() // 完成项目弹窗引用
  const queryParams = reactive({
      pageNo: 1,
    pageSize: 10,
    projectNumber: undefined,
    projectType: undefined,
    taxpayerAttribute: undefined,
    isKeyEnterprise: undefined,
    companyName: undefined,
    status: undefined,
    projectStatus: undefined,
  })
  const queryFormRef = ref() // 搜索的表单

  /** 查询列表 */
  const getList = async () => {
    loading.value = true
    try {
      const data = await ProjectManagementApi.getProjectManagementTodoPage(queryParams)
      list.value = data
      total.value = data.total
    } finally {
      loading.value = false
    }
  }

  /** 搜索按钮操作 */
  const handleQuery = () => {
    queryParams.pageNo = 1
    getList()
  }

  /** 重置按钮操作 */
  const resetQuery = () => {
    queryFormRef.value.resetFields()
    handleQuery()
  }

  /** 添加操作 */
  const handleCreate = () => {
    router.push({ name: 'ProjectManagementCreate' })
  }

  /** 详情操作 */
  const handleDetail = (row: ProjectManagementVO) => {
    router.push({
      name: 'ProjectManagementDetail',
      query: {
        id: row.id
      }
    })
  }

  /** 完成项目操作 */
  const handleComplete = async (row) => {
    completeDialogRef.value.open(row)
  }

  /** 完成项目成功回调 */
  const handleCompleteSuccess = () => {
    getList()
  }

  /** 取消请假操作 */
  const cancelProcess = async (row) => {
    // 二次确认
    const { value } = await ElMessageBox.prompt('请输入取消原因', '取消流程', {
      confirmButtonText: t('common.ok'),
      cancelButtonText: t('common.cancel'),
      inputPattern: /^[\s\S]*.*\S[\s\S]*$/, // 判断非空，且非空格
      inputErrorMessage: '取消原因不能为空'
    })
    // 发起取消
    await ProcessInstanceApi.cancelProcessInstanceByStartUser(row.id, value)
    message.success('取消成功')
    // 刷新列表
    await getList()
  }

  /** 审批进度 */
  const handleProcessDetail = (row) => {
    router.push({
      name: 'BpmProcessInstanceDetail',
      query: {
        id: row.processInstanceId
      }
    })
  }

  /** 导入按钮操作 */
  const beforeImport = async (file: File) => {
    try {
      const formData = new FormData()
      formData.append('file', file)
      await ProjectManagementApi.importProjectManagement(formData)
      message.success('导入成功')
      await getList()
    } catch (error: any) {
      message.error('导入失败：' + (error.message || '未知错误'))
    }
    return false // 阻止默认上传行为
  }

  /** 下载模板按钮操作 */
  const downloadTemplate = async () => {
    try {
      const data = await ProjectManagementApi.downloadImportTemplate()
      download.excel(data, '项目管理_导入模板.xls')
    } catch (error: any) {
      message.error('下载模板失败：' + (error.message || '未知错误'))
    }
  }

  // fix: 列表不刷新的问题。
  watch(
      () => router.currentRoute.value,
      () => {
        getList()
      }
  )

  /** 初始化 **/
  onMounted(() => {
    getList()
  })
</script>
